package bluetoothcomm;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;  

import monitor.StreamEvent;
import monitor.StreamEventListener;

import bluetoothcomm.SerialWriter;


import snd.SndPlayerMbed;

public class MainWindow implements  ActionListener, StreamEventListener{

	
//	GraphicsPanel panelDisplay;
	JPanel panelBtn = new JPanel();
	JButton btnStart = new JButton("Start");
	JButton btnStop = new JButton("Stop");
	JButton btnSound = new JButton("Sound");
	JButton btnDrive = new JButton("Drive");
	JButton btnSpin = new JButton("Spin");
	JButton btnCircle = new JButton("Circle");
	JButton btnPlayNote = new JButton("Play note");
	JButton btnCaptureImage = new JButton("Capture Image");
	JButton btnStopDrive = new JButton("Stop Drive");
	JButton btnIncVol = new JButton("Increase Volume");
	JButton btnDecVol = new JButton("Decrease Volume");
	
	JFrame frame = new JFrame();
	static TwoWayComm twoWayComm;
	static SndPlayerMbed sndPlayer;
	public static SerialWriter roombaComm;

	ArrayList<Integer> test = new ArrayList<Integer>();
   
   public MainWindow(){

	//   panelDisplay = new GraphicsPanel();
	   
	   panelBtn.setSize(100,480);
	   panelBtn.setLocation(0,0);
	   
	   btnStart.addActionListener(this); 
	   btnStop.addActionListener(this); 
	   btnSound.addActionListener(this); 
	   btnDrive.addActionListener(this); 
	   btnSpin.addActionListener(this); 
	   btnCircle.addActionListener(this); 
	   btnPlayNote.addActionListener(this);
	   btnCaptureImage.addActionListener(this);
	   btnStopDrive.addActionListener(this);
	   btnIncVol.addActionListener(this);
	   btnDecVol.addActionListener(this);
	   
	   panelBtn.add(btnStart);
	   panelBtn.add(btnStop);
	   panelBtn.add(btnSound);
	   panelBtn.add(btnDrive);
	   panelBtn.add(btnSpin);
	   panelBtn.add(btnCircle);
	   panelBtn.add(btnPlayNote);
	   panelBtn.add(btnCaptureImage);
	   panelBtn.add(btnStopDrive);
	   panelBtn.add(btnIncVol);
	   panelBtn.add(btnDecVol);
	   
	   frame.setSize(740, 480); 
	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   frame.getContentPane().setLayout(new BorderLayout());
	//   frame.getContentPane().add(panelDisplay); // add the panel to frame   
	   frame.getContentPane().add(panelBtn);
	   frame.setVisible(true); // display this frame
	   
	   test.add(7);
	   test.add(2);
	   test.add(3);
	   test.add(4);
	   test.add(1);
   }
   
   @Override
   public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
	 
	   Object source = event.getSource();	  
	   if (source == btnStart)
	   {		   
		   System.out.println("Start bluetooth");  
	   }
	   else if (source == btnStop)
	   {		   	   
		   twoWayComm.close();
		   System.out.println("Stop bluetooth");
	   }
	   else if (source == btnSound)
	   {		   		   
		   /*sndPlayer.playWait();
		   sndPlayer.playLost();
		   sndPlayer.playWait();
		   sndPlayer.playLost();*/
		   sndPlayer.playLocate(test);
		   sndPlayer.playLost();
		 //  roombaComm.stop(); 
		   System.out.println("Play sound");	  
	   }
	   else if (source == btnDrive)
	   {		   		   
		   System.out.println("Drive");
		   roombaComm.smartCurveDrive(0, 550,130);
	   }
	   else if (source == btnSpin)
	   {		   
		   System.out.println("Spin");  
		   roombaComm.spinRight();
	   }
	   else if (source == btnCircle)
	   {		   
		   System.out.println("Circle");
		   
	   }
	   else if (source == btnPlayNote)
	   {		   
		   System.out.println("Play Note");
		   roombaComm.playNote(100,10);
	   }
	   else if (source == btnCaptureImage)
	   {
		   System.out.println("Capture Image");
		   SerialReader.isCapturingImage = true;
	   }
	   else if (source == btnStopDrive)
	   {
		   System.out.println("Stop Drive");
		   roombaComm.drive(0,0);
	   }
	   else if (source == btnIncVol)
	   {
		   System.out.println("Increase Volume");
		   sndPlayer.increaseVol();
	   }
	   else if (source == btnDecVol)
	   {
		   System.out.println("Decrease Volume");
		   sndPlayer.decreaseVol();
	   }
	   
   }
   
   public static void main(String[] args)
   {
		twoWayComm = new TwoWayComm();
		MainWindow myWindow = new MainWindow();
		   
		try {
				twoWayComm.connect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		sndPlayer = new SndPlayerMbed();
		
		roombaComm = new SerialWriter();
	
		sndPlayer.playWait();
		roombaComm.stop();
		
		twoWayComm.sendMessageToPebble("StartNavigation");
		
		
   }

@Override
public void streamAvailable(StreamEvent e) {
	// TODO Auto-generated method stub
	System.out.println(e.getStr());
}
}


